package com.xidian.zhiancloud.controller.workTicket;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.dto.workTicket.HighTicketVODTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.workTicket.HighTicket;
import com.xidian.zhiancloud.qo.workTicket.HighTicketQO;
import com.xidian.zhiancloud.service.workTicket.HighFlowService;
import com.xidian.zhiancloud.service.workTicket.HighService;
import com.xidian.zhiancloud.util.zyf.RedisUtil;
import com.xidian.zhiancloud.vo.workTicket.HighTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

@RestController
@RequestMapping("High")
public class HighController extends BaseController {
    @Autowired
    private HighService highService;

    @Autowired
    private HighFlowService highFlowService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 申请作业票
     *
     * @param highTicket
     * @return
     */
    @PostMapping("/addHighTicket")
    public ResultDTO<Boolean> addHighTicket(HighTicket highTicket) {
        highTicket.setCompanyId(getSessionUserCompanyId());
        highTicket.setApplicantId(getSessionUserid());
        highTicket.setCreateTime(new Date(System.currentTimeMillis()));
//        highTicket.setApplicantUnitId();
        // 设置危险作业票id
        highTicket.setWorkNumber(redisUtil.generateWorkId(getSessionUserCompanyId(), highTicket.getApplicantUnitId()));
        highTicket.setIsCommit1(1);
        highTicket.setCurrentStep(2);
        Boolean res = highService.save(highTicket);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除待提交作业票
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteHighTicket")
    public ResultDTO<Boolean> deleteHighTicket(@RequestParam("ids") Long[] ids) {
        if (ids == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        Boolean res = highService.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id查询作业票
     *
     * @param id
     * @return
     */
    @GetMapping("/getHighWorkTicket")
    public ResultDTO<HighTicketVODTO> getHighWorkTicket(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        Long companyId = getSessionUserCompanyId();
        // 返回DTO对象，为highTicketVO及操作权限标识Map
        HighTicketVODTO highTicketVODTO = highService.getHighTicketVODTOById(companyId, id);
        return ResultDTO.successOf(highTicketVODTO, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查找
     *
     * @param highTicketQO
     * @return
     */
    @GetMapping("getHighWorkTicketList")
    public ResultDTO<IPage<HighTicketVO>> getHighWorkTicketList(HighTicketQO highTicketQO) {
        if (highTicketQO.getSize() == null || highTicketQO.getCurrent() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        highTicketQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(highService.getListByQO(highTicketQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 作业票审批
     *
     * @param highTicket
     * @return
     */
    @PostMapping("/approveHighTicket")
    public ResultDTO<Boolean> approveHighTicket(HighTicket highTicket) {
        Long ticketId = highTicket.getId();
        // 前端的fireTicket在根据id更新后端的记录时，不对以下字段做修改
        highTicket.setUpdateTime(new Date(System.currentTimeMillis()));
        highTicket.setCompanyId(null);
        highTicket.setCreateTime(null);
        highTicket.setIsCommit1(null);
        highTicket.setIsCommit2(null);
        highTicket.setIsCommit3(null);
        highTicket.setIsCommit4(null);
        highTicket.setIsCommit5(null);
        highTicket.setIsCommit6(null);
        highTicket.setIsCommit7(null);
        highTicket.setIsCommit8(null);
        highTicket.setIsCommit9(null);
        highTicket.setIsCommit10(null);
        highTicket.setIsCommit11(null);
        highTicket.setIsCommit12(null);
        highTicket.setIsCommit13(null);
        highTicket.setIsCommit14(null);
        // 根据id查询数据库已有数据
        HighTicket highTicketData = highService.getById(ticketId);
        Integer currentStep = highTicketData.getCurrentStep();

        Date nowTime = new Date(System.currentTimeMillis());

        //  判断是哪一个小步骤完成了提交
        Integer subStep = highFlowService.getFinishSubId(currentStep, highTicket);
        // 将该小步骤步中的isCommit**设置为1，并填充时间
        highService.setSubStepFinish(subStep, highTicket);

        // 保存
        Boolean res1 = highService.updateById(highTicket);
        // 获取保存后的ticket
        HighTicket newHighTicket = highService.getById(ticketId);
        // 判断本阶段作业流是否已经结束
        Integer updateStep = highFlowService.updateCurrentStep(currentStep, newHighTicket);
        // 更新当前大步骤的状态
        newHighTicket.setCurrentStep(updateStep);
        Boolean res = highService.updateById(newHighTicket);
        return ResultDTO.successOf(res || res1, GeneralResponseEnums.SUCCESS);
    }

}
